package pong.game.objects;

import java.util.Vector;

import pong.game.WifiType;
import pong.general.GameWindow;

import android.graphics.Bitmap;

public class Handle extends Rectangle
{
	void init()
	{
		GRAVITY = 0.0f;
		DRAG 	= 0.6f;
		BOUNCE 	= 0.1f;
	}
	
	public Handle(Bitmap handle)
	{
		super(handle);
		init();
	}
	public Handle(Bitmap handle, WifiType wifiType, float X, float Y, float height, float width)
	{
		super(handle, wifiType, X, Y, height, width);
		init();
	}
	public Handle(Bitmap handle, WifiType wifiType, float X, float Y)
	{
		super(handle, wifiType, X, Y, (float)handle.getHeight() / GameWindow.heightWorld,
				(float)handle.getWidth() / GameWindow.widthWorld);
		init();
	}

	boolean 		leaveGameZone(Vector<DinamicObject> GameObjects)
    {
        return pos.x < 0 || pos.x + width > 1.0f;
    }	
	void			addPosition(Vector<DinamicObject> GameObjects)
	{
		dir.y = 0;
		pos.add(dir);
		if (leaveGameZone(GameObjects) || isConflict(GameObjects))
        {
			if(pos.x <= 0)
			{
				dir.minus(dir);
				pos.x = 0;
				return;
			}
			if(pos.x + width >= 1.0f)
			{
				dir.minus(dir);
				pos.x = 1.0f - width;
				return;
			}
			pos.minus(dir);
			pos.minus(dir);
			
			addPosition(GameObjects);
        }
	}
	
	
	
	// Physic Engine
	/////////////////////////////////
	
	public void updateVelocity(float x, float y)
	{
		dir.x = x;
		dir.y = 0;
	}
	public void updatePos(float x, float y)
	{
		pos.x = x;
	}
	public synchronized void checkWallCollisions(float widthWorld)
	{
		float maxY = 1.0f - radius * 2;
		float maxX = widthWorld - radius * 2;
		
		if (pos.y > maxY) {
			updatePos(pos.x, maxY);
			updateVelocity(dir.x, (dir.y * -BOUNCE));
		}
		
		if (pos.y < 0) {
			updatePos(pos.x, 0);
			updateVelocity(dir.x, (dir.y * -BOUNCE));
		}
		
		if (pos.x > maxX) {
			updatePos(maxX, pos.y);
		}
		if (pos.x < 0) {
			updatePos(0, pos.y);
		}
	}
}
